package com.company.controller;

import com.company.CompanyException;
import com.company.model.Comment;
import com.company.model.Company;
import com.company.model.request.CommentForm;
import com.company.model.request.CommentQuery;
import com.company.model.request.CompanyForm;
import com.company.model.request.CompanyQuery;
import com.company.model.result.BaseResult;
import com.company.model.result.CompanyResult;
import com.company.model.result.CompanySearchResult;
import com.company.model.result.PageResult;
import com.company.service.ICommentService;
import com.company.service.ICompanyService;
import com.company.utils.RequestUtil;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by tu on 2017/1/1.
 */
@Controller
@RequestMapping(value = "/companies")
public class CompanyController {

  @Autowired
  ICompanyService service;
  @Autowired
  ICommentService commentService;

  /**
   * 添加公司
   *
   * @param company {@link CompanyForm}
   * @return {@link BaseResult<PageResult<Comment>>}
   */
  @PostMapping
  public String add(Model model, CompanyForm company, HttpServletRequest request,
      BindingResult bindingResult)
      throws CompanyException {
    String userAgent = RequestUtil.getUserAgent(request);
    Company info = service.add(company, userAgent);
    return company(model, info.getId());
  }

  @GetMapping(value = "/create")
  public String add(Model model, String name)
      throws CompanyException {
    model.addAttribute("name", name);
    return "company/create";
  }

  /**
   * 公司列表
   *
   * @return {@link BaseResult<PageResult<Company>>}
   */
  @RequestMapping(method = RequestMethod.GET)
  public String companies(Model model, String keyword, Long id, Integer sort) {
    CompanyQuery query = new CompanyQuery();
    query.setName(keyword);
    query.setId(id);
    query.setSort(sort);
    query.setPageSize(1000);
    PageResult<Company> companies = service.list(query);

    if(ObjectUtils.isEmpty(companies.getContent())){
      return add(model,keyword);
    }

    CompanySearchResult companySearchResult = new CompanySearchResult(keyword,
        companies.getContent());
    BaseResult<CompanySearchResult> result = new BaseResult();
    result.setData(companySearchResult);
    result.setCode(
        ObjectUtils.isEmpty(companySearchResult.getCompanies()) ? HttpStatus.NO_CONTENT.value()
            : HttpStatus.OK.value());
    model.addAttribute("result", result);

    return "company/list";
  }

  /**
   * 公司信息
   *
   * @param id 公司ID
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public String company(Model model, @PathVariable("id") Long id) throws CompanyException {
    CompanyQuery query = new CompanyQuery();
    query.setId(id);
    Company companyInfo = service.find(query);
    CommentQuery commentQuery = new CommentQuery();
    commentQuery.setCompanyId(companyInfo.getId());

    BaseResult result = new BaseResult<CompanyResult>();
    CompanyResult companyResult = new CompanyResult();
    companyResult.setCompany(companyInfo);
    PageResult<Comment> commentPageResult = commentService.list(commentQuery);
    if (null != commentPageResult) {
      companyResult.setComments(commentPageResult.getContent());
      result.setCode(
          ObjectUtils.isEmpty(commentPageResult.getContent()) ? HttpStatus.NO_CONTENT.value()
              : HttpStatus.OK.value());
    }
    result.setData(companyResult);
    model.addAttribute("result", result);
    return "company/detail";
  }

  /**
   * 添加评论
   *
   * @param comment {@link CommentForm}
   * @param request {@link HttpServletRequest}
   * @return {@link BaseResult<PageResult<Comment>>}
   * @throws CompanyException {@link CompanyException}
   */
  @PostMapping(value = "/{id}/comment")
  public String add(@PathVariable("id") Long companyId, CommentForm comment, Model model,
      HttpServletRequest request) throws CompanyException {
    String userAgent = RequestUtil.getUserAgent(request);
    comment.setCompanyId(companyId);
    commentService.add(comment, userAgent);

    CommentQuery query = new CommentQuery();
    query.setCompanyId(comment.getCompanyId());
    query.setParentId(comment.getParentId());

    return company(model, companyId);
  }
}